// Testa se as janelas funcionam

#include <window.h>
#include <timer.h>
#include <stdio.h>
#include <stdlib.h>
#include <sprite.h>
#include <pixmap.h>
#include <kbc.h>
#include <xpm/cloud.xpm>

/* Imagem */
int i_w, i_h;
char *img_data;

/* Funções de desenho */
static void draw_color_func (Window win, int x, int y, int w, int h, void *data) {
	window_fill (win, (int) data);
}

static void draw_image_func (Window win, int x, int y, int w, int h, void *data) {
	window_image_corner (win, img_data, i_w, i_h);
}

static void draw_lines_func (Window win, int x, int y, int w, int h, void *data) {
	// Quadrado
	window_draw_line (win, x, y, x, y + h - 1, (int) data);
	window_draw_line (win, x, y, x + w - 1, y, (int) data);
	window_draw_line (win, x + w - 1 , y, x + w - 1, y + h - 1, (int) data);
	window_draw_line (win, x , y + h - 1, x + w - 1, y + h - 1, (int) data);

	// Linhas no meio
	window_draw_line (win, x + w/2, y, x+w/2, y + h - 1, (int) data);
	window_draw_line (win, x, y+h/2, x + w - 1, y+h/2, (int) data);

	// Diagonais
	window_draw_line (win, x, y, x + w - 1, y + h - 1, (int) data);
	window_draw_line (win, x, y, x + w/2, y + h - 1, (int) data);
	window_draw_line (win, x, y, x + w - 1, y + h/2, (int) data);
	window_draw_line (win, x + w - 1, y, x, y + h - 1, (int) data);
	return;


	window_draw_line (win, x + w - 1, y + h - 1, x + w - 1, y + h - 1, (int) data);
}

static void draw_int_func (Window win, int x, int y, int w, int h, void *data) {
	window_draw_int (win, -1234567890/*(int) data*/, 0, 0, 15);
}

int main () {
	Window win;
	int i;

	puts ("Inicializacao, finalizacao e criacao de janelas");
	puts ("Varias janelas serao criadas com cores e posicoes aleatorias\n");
	puts ("Testamos:");
	puts ("\twinsys_init ()");
	puts ("\twinsys_draw ()");
	puts ("\twindow_show ()");
	puts ("\twindow_fill ()");
	mili_sleep (1000);

	winsys_init (R640x480);

	win = window_new (-10, -10, 128, 96, (void *) 72);
	window_show (win, draw_color_func);
	winsys_draw ();
	mili_sleep(1000);
	for (i = 0; i < 20; i++) {
		Window win = window_new (rand() % 640, rand() % 480, 128, 96, (void*) (rand()%256));
		window_show (win, draw_color_func);
		mili_sleep (500);
		winsys_draw ();
	}
	winsys_textmode();

	puts ("Agora vamos trocas a resolucao. As mesmas janelas devem ser exibidas");
	puts ("Testamos:");
	puts ("\twinsys_textmode ()");
	puts ("\twinsys_graphicmode ()");
	mili_sleep(1000);
	winsys_graphicmode(R1280x1024);
	winsys_draw ();
	mili_sleep(2000);  
	winsys_finish();

	puts ("Finalizamos o sistema de janelas. Todas as janelas devem ter sido destruidas");
	puts ("Testamos:");
	puts ("\twinsys_finish ()");
	mili_sleep(1000);
	winsys_init (R640x480);
	winsys_draw ();
	mili_sleep(2000);
	winsys_finish();

	puts ("Uma janela sera criada e ela devera atravessar o ecran");
	puts ("Testamos:");
	puts ("\twindow_move ()");
	mili_sleep(1000);
	winsys_init (R640x480);
	win = window_new (640, 200, 128, 96, (void *) 72);
	window_show (win, draw_color_func);
	for (i = 640; i > -128; i-=5) {
		window_move (win, i, 200);
		winsys_draw ();
		mili_sleep (20);
	}
	winsys_finish();

	puts("Testa scrolling. O efeito deve ser o mesmo que no teste anterior");
	puts ("Testamos:");
	puts ("\twinsys_set_diff_coord ()");
	mili_sleep(1000);
	winsys_init (R640x480);
	win = window_new (640, 200, 128, 96, (void *) 72);
	window_show (win, draw_color_func);
	for (i = 0; i < 640 + 128; i+= 5) {
		winsys_set_diff_coord (5, 0);
		winsys_draw ();
		mili_sleep (20);
	}
	winsys_finish();

	puts ("Testa imagens. Uma imagem com transparencia e mostrada sobre uma cor de fundo");
	puts ("Testamos:");
	puts ("\t window_image ()");
	puts ("\t window_image_corner ()");
	mili_sleep(1000);
	
	// carrega a imagem
	img_data = read_xpm_gimp (cloud_xpm, &i_w, &i_h);

	winsys_init (R640x480);

	// Fundo
	win = window_new (0, 0, 640, 480, (void *) 1);
	window_show (win, draw_color_func);
	// Frente
	win = window_new (640, 200, i_w, i_h, NULL);
	window_show (win, draw_image_func);
	for (i = 640; i > -128; i-=2) {
		window_move (win, i, 200);
		winsys_draw ();
		mili_sleep (20);
	}
	winsys_finish();
	return 0;
	puts ("Desenho de linhas, similar ao primeiro teste");
	puts ("Testamos:");
	puts ("\twindow_draw_line ()");
	puts ("\twindow_draw_point ()");
	mili_sleep (1000);

	winsys_init (R640x480);

	win = window_new (-10, -10, 128, 96, (void *) 72);
	window_show (win, draw_lines_func);
	winsys_draw ();
	mili_sleep(1000);
	for (i = 0; i < 20; i++) {
		Window win = window_new (rand() % 640, rand() % 480, 128, 96, (void*) (rand()%256));
		window_show (win, draw_lines_func);
		mili_sleep (500);
		winsys_draw ();
	}
	winsys_finish();

	puts ("Desenho de caracters, strings e números");
	puts ("Testamos:");
	puts ("\twindow_draw_char ()");
	puts ("\twindow_draw_point ()");
	mili_sleep (1000);

	winsys_init (R640x480);

	win = window_new (-10, -10, 120, 15, (void *) 72);
	window_show (win, draw_int_func);
	winsys_draw ();
	mili_sleep(1000);
	for (i = 0; i < 20; i++) {
		Window win = window_new (rand() % 640, rand() % 480, 120, 15, (void*) (rand()%256));
		window_show (win, draw_int_func);
		mili_sleep (500);
		winsys_draw ();
	}
	winsys_finish();

	return 0;
}

